﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SummaryForm.xaml
    /// </summary>
    public partial class Purchase : DocumentBase
    {
        #region-------------Properties-----------------------
        INTEXDataSet iNTEXDataSet;
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        //private Dictionary<string, object> dicQueryParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        public DataSet dsParamaters;
        PurchaseData m_Data;
        public DataRow dwABPOS;
        private string m_GSART, m_GSVAR, m_GSCOLOR, m_Supplier;
        private string fromorderdate = "", endorderdate = ""
            , fromincomedate = "", endincomedate = "", openorderdate1 = "", openorderdate2 = "";
        // string OrderNumber="";
        
        #endregion
        public Purchase()
        {
            InitializeComponent();
            this.Name = "采购报表";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
           

            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new PurchaseData(dicParamater);
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        } 
        private void InitDataBinding()
        {
            try
            { 

                this.setUI.Tables.Clear();
                this.setUI.Tables.Add("GSDAT");
                this.setUI.Tables.Add("GSCOLOR");
                this.setUI.Tables.Add("LIEFDAT");
                orlReadCommand = OracleDB.CreateCommand();

                #region "GSDAT"
                orlReadCommand.CommandText = "SELECT distinct GSART,GSVAR FROM INTEX.GSDAT WHERE GSMANDANT='00' ORDER BY 1";

                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "GSDAT";
                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    }
                }
                #endregion

                #region "GSCOLOR"
                orlReadCommand.CommandText = "SELECT distinct GSCART,GSCVAR,GSCFARBE,GSCBEZ FROM INTEX.GSCOLOR  WHERE GSCMANDANT='00' ORDER BY 1";

                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "GSCOLOR";

                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        this.setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    }

                }
                #endregion
                #region "LIEFDAT"
                orlReadCommand.CommandText = "SELECT distinct LSLIEFNR,LSLIEFNA,LSNAME1 FROM INTEX.LIEFDAT WHERE LSMANDANT='00' ORDER BY 1";

                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "LIEFDAT";

                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        this.setUI = m_Data.ImportDataTable(setUI, dtTemp);
                    }

                }
                #endregion
                setUI.AcceptChanges();
                foreach (DataTable dtTemp in setUI.Tables)
                {
                    iNTEXDataSet.Merge(dtTemp);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            iNTEXDataSet = ((INTEXDataSet)(this.FindResource("iNTEXDataSet")));
            InitDataBinding();


            // Load data into the table EKPOS. You can modify this code as needed.
            // IntexReportClient.INTEXDataSetTableAdapters.GSCOLORTableAdapter iNTEXDataSetGSCOLORableAdapter = new IntexReportClient.INTEXDataSetTableAdapters.GSCOLORTableAdapter();
            // iNTEXDataSetGSCOLORableAdapter.Fill(iNTEXDataSet.GSCOLOR);
            // Load data into the table LIEFDAT. You can modify this code as needed.
            // IntexReportClient.INTEXDataSetTableAdapters.LIEFDATTableAdapter iNTEXDataSetLIEFDATTableAdapter = new IntexReportClient.INTEXDataSetTableAdapters.LIEFDATTableAdapter();
            // iNTEXDataSetLIEFDATTableAdapter.Fill(iNTEXDataSet.LIEFDAT);

        }

        private void PurchaseMaterial_Click(object sender, RoutedEventArgs e)
        {
            this.PurchaseMaterial.Visibility = Visibility.Collapsed;
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM PURCHASE_MATERIAL_REPORT WHERE (ART = :ART OR :ART IS NULL) "
                    + " AND (VAR = :VAR OR :VAR IS NULL)  AND (FARBE = :FARBE OR :FARBE IS NULL) "
                    + " AND (LIEFNR = :LIEFNR OR :LIEFNR IS NULL)   AND ( ORDERDATE > :FROMORDERDATE OR :FROMORDERDATE IS NULL) "
                    + " AND (ORDERDATE < :ENDORDERDATE OR :ENDORDERDATE IS NULL)  AND (INCOMEDATE > :FROMINCOMEDATE OR :FROMINCOMEDATE IS NULL) "
                    + " AND (INCOMEDATE < :ENDINCOMEDATE OR :ENDINCOMEDATE IS NULL) "
                                ;
                orlReadCommand.Parameters.Add("ART", OracleType.NVarChar).Value = this.GSARTComboBox.Text.ToString();
                orlReadCommand.Parameters.Add("VAR", OracleType.NVarChar).Value = this.GSVARComboBox.Text.ToString();
                orlReadCommand.Parameters.Add("FARBE", OracleType.NVarChar).Value = this.GSCFARBEComboBox.Text.ToString();
                orlReadCommand.Parameters.Add("LIEFNR", OracleType.NVarChar).Value = this.LSLIEFNRComboBox.Text.ToString();
                orlReadCommand.Parameters.Add("FROMORDERDATE", OracleType.NVarChar).Value = fromorderdate;
                orlReadCommand.Parameters.Add("ENDORDERDATE", OracleType.NVarChar).Value = endorderdate;
                orlReadCommand.Parameters.Add("FROMINCOMEDATE", OracleType.NVarChar).Value = fromincomedate;
                orlReadCommand.Parameters.Add("ENDINCOMEDATE", OracleType.NVarChar).Value = endincomedate;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "EKPOS";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["EKPOS"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["EKPOS"].Columns)
                    {
                        if (dc.ColumnName == "DES")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "COLOR")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }


                PurchaseReport m_rpt = new PurchaseReport(dicParamater);
                m_rpt.dsSource = dsResult;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.PurchaseMaterial_TO_EXCEL("EXCEL");

                this.PurchaseMaterial.Visibility = Visibility.Visible;
                m_Data = new PurchaseData(dicParamater);
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;
                RefreshUI();
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void RefreshUI()
        {

            try
            {
                this.GSARTComboBox.SelectionChanged -= new SelectionChangedEventHandler(GSARTComboBox_SelectionChanged);
                this.GSVARComboBox.SelectionChanged -= new SelectionChangedEventHandler(GSVARComboBox_SelectionChanged);
                this.GSARTComboBox.SelectedIndex = -1;
                this.GSVARComboBox.SelectedIndex = -1;
                this.GSCFARBEComboBox.SelectedIndex = -1;
                this.LSLIEFNRComboBox.SelectedIndex = -1;
                this.GSARTComboBox.SelectionChanged += new SelectionChangedEventHandler(GSARTComboBox_SelectionChanged);
                this.GSVARComboBox.SelectionChanged += new SelectionChangedEventHandler(GSVARComboBox_SelectionChanged);
                this.GSARTComboBox.Text = "";
                this.GSVARComboBox.Text = "";
                this.GSCFARBEComboBox.Text = "";
                this.LSLIEFNRComboBox.Text = "";
                this.FromOrderDate.Text = "";
                this.EndOrderDate.Text = "";
                this.FromIncomeDate.Text = "";
                this.EndIncomeDate.Text = "";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void GSARTComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                GSVARComboBox.Items.Clear();
                ComboBox cb = (ComboBox)sender;
                string gsart;

                gsart = ((DataRowView)cb.SelectedItem).Row["GSART"].ToString().Trim();
                m_GSART = gsart;
                DataTable tableGSDAT = iNTEXDataSet.Tables["GSDAT"];
                var query = from gscolor in tableGSDAT.AsEnumerable()
                            where gscolor.Field<string>("GSART").StartsWith(gsart)
                            select gscolor;

                foreach (DataRow rowTemp in query)
                {
                    GSVARComboBox.Items.Add(rowTemp["GSVAR"].ToString());
                }
                /*
                this.GSVARComboBox.DataContext = from gscolor in tableGSDAT.AsEnumerable()
                                                 where gscolor.Field<string>("GSART").StartsWith(gsart)
                                                  select gscolor;
                GSVARComboBox.ItemsSource= from gscolor in tableGSDAT.AsEnumerable()
                                      where gscolor.Field<string>("GSART").StartsWith(gsart)
                                      select gscolor;
                */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void GSVARComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                GSCFARBEComboBox.Items.Clear();
                ComboBox cb = (ComboBox)sender;
                string gsart, gsvar;

                gsvar = cb.SelectedItem.ToString();
                m_GSVAR = gsvar;
                DataTable tableGSDAT = iNTEXDataSet.Tables["GSCOLOR"];
                var query = from gscolor in tableGSDAT.AsEnumerable()
                            where gscolor.Field<string>("GSCART").StartsWith(m_GSART)
                            && gscolor.Field<string>("GSCVAR").StartsWith(m_GSVAR)
                            select gscolor;

                foreach (DataRow rowTemp in query)
                {
                    this.GSCFARBEComboBox.Items.Add(rowTemp["GSCFARBE"].ToString());
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Supp_Pur_Summ_Click(object sender, RoutedEventArgs e)
        {
            this.Supp_Pur_Summ.Visibility = Visibility.Collapsed;
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT LIEFNR,SUPPLIER,SUM(ORDERQTY) AS QUANTITY, "
                    + " SUM(VALUE) AS VALUE  FROM PURCHASE_REPORT  "
                    + " WHERE (ORDERDATE > :OPENORDERDATE1 OR :OPENORDERDATE1 IS NULL) "
                    + " AND (ORDERDATE < :OPENORDERDATE2 OR :OPENORDERDATE2 IS NULL)  GROUP BY LIEFNR,SUPPLIER "
                                ;

                orlReadCommand.Parameters.Add("OPENORDERDATE1", OracleType.NVarChar).Value = openorderdate1;
                orlReadCommand.Parameters.Add("OPENORDERDATE2", OracleType.NVarChar).Value = openorderdate2;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "PURCHASE_SUPP_SUMM";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["PURCHASE_SUPP_SUMM"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["PURCHASE_SUPP_SUMM"].Columns)
                    {

                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }

                PurchaseReport m_rpt = new PurchaseReport(dicParamater);
                m_rpt.dsSource = dsResult;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.Supp_Pur_Summ_TO_EXCEL("EXCEL");
                this.OpenOrderDate1.Text = "";
                this.OpenOrderDate2.Text = "";
                this.Supp_Pur_Summ.Visibility = Visibility.Visible;
                m_Data = new PurchaseData(dicParamater);
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }


        } 
        private void FromOrderDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {

            try
            {

                fromorderdate =m_Data.GetDate(sender);

            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }



        }

        private void EndOrderDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                endorderdate =m_Data.GetDate(sender);

            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }

        }

        private void FromIncomeDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                fromincomedate = m_Data.GetDate(sender);


            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }

        private void EndIncomeDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            { 
                endincomedate =m_Data.GetDate(sender); 
            } 
            catch (Exception ex)
            { 
                Tracer.Error(ex.StackTrace, ex); 
            }
        }

        private void OpenOrderDate1_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                openorderdate1 =m_Data.GetDate(sender);

            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }

        private void OpenOrderDate2_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                openorderdate2 =m_Data.GetDate(sender);

            }

            catch (Exception ex)
            {

                Tracer.Error(ex.StackTrace, ex);

            }
        }

        private void Pur_Mat_List_Click(object sender, RoutedEventArgs e)
        {
            this.Pur_Mat_List.Visibility = Visibility.Collapsed;
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM PURCHASE_REPORT "
                                ;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "Pur_Mat_List";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["Pur_Mat_List"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["Pur_Mat_List"].Columns)
                    {
                        if (dc.ColumnName == "DES")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "COLOR")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }

                PurchaseReport m_rpt = new PurchaseReport(dicParamater);
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.dsSource = dsResult;
                m_rpt.Pur_Mat_List_TO_EXCEL("EXCEL");
                this.Pur_Mat_List.Visibility = Visibility.Visible;
                m_Data = new PurchaseData(dicParamater);
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;
            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void Material_Inventory_Click(object sender, RoutedEventArgs e)
        {
            this.Material_Inventory.Visibility = Visibility.Collapsed;
            DataSet dsResult = new DataSet();
            dsResult.Clear();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT CODE,GLLB,ART,VAR,COLOR,SUPPLIER,INCOMEDATE,BATCH,SUPPLIERBATCH, "
                + " SUM(WEI) AS WEI FROM INVENTORY_YARN_REPORT_LB GROUP BY CODE,GLLB,ART,VAR,COLOR,SUPPLIER,INCOMEDATE,BATCH,SUPPLIERBATCH "
                                ;

                dsResult = m_Data.Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "INVENTORY_YARN";
                }
                byte[] b;
                string value;
                foreach (DataRow dw in dsResult.Tables["INVENTORY_YARN"].Rows)
                {
                    foreach (DataColumn dc in dsResult.Tables["INVENTORY_YARN"].Columns)
                    {
                        if (dc.ColumnName == "COLOR")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }
                        if (dc.ColumnName == "SUPPLIER")
                        {
                            b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[dc.ColumnName].ToString().Trim());
                            value = Encoding.GetEncoding("gb2312").GetString(b);
                            dw[dc.ColumnName] = value;
                        }

                    }
                }

                PurchaseReport m_rpt = new PurchaseReport(dicParamater);
                m_rpt.dsSource = dsResult;
                m_rpt.exAgent = m_Data.exAgent;
                m_rpt.Material_Inventory_TO_EXCEL("EXCEL");
                this.Material_Inventory.Visibility = Visibility.Visible;
                m_Data = new PurchaseData(dicParamater);
                m_Data.OracleDB = this.OracleDB;
                m_Data.proxy = this.proxy;

            }

            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        } 
    }
}